package com.lqian.simplestructure.linearlist;

import java.util.Arrays;

/**
 * @author：Melody_LQ
 * @time：2022/7/22 12:26 上午
 */
public class Solution {

    public static void main(String[] args) {
        //数组创建以后长度不变，定义新的数组添加长度
        //去除重复的数组
        Solution p1 = new Solution();
        Object[] array = new String[] {"a","a","b","c","c","d"};
        System.out.println("原来的数组内容为：" + Arrays.toString(array));
        array = p1.deleteDuplicateElement(array);
        System.out.println("修改过的数组为：" + Arrays.toString(array));

    }

    public static Object[] deleteDuplicateElement(Object[] array) {
        //去除重复的数组元素
        /**
         * 1.两层循环把不重复的数组添加到另外一个数组并且记录长度
         * 2.再把零食数组的值赋值给另外一个数组返回
         */
        int length = 0;
        Object[] tempArray = new Object[array.length ];
        for(int i = 0;i < array.length ;i++) {
            boolean isDuplicate= false;
            for(int j = i+1;j < array.length ; j++ ) {
                if(array[i] ==array[j] || array[i].equals(array[j])) {
                    isDuplicate = true;
                    break;
                }
            }
            if(!isDuplicate) {
                tempArray[length] = array[i];
                length++;
            }
        }
        Object[] newArray = new Object[length];
        System.arraycopy(tempArray, 0, newArray, 0, length);
        return newArray;
    }
}
